//
//  CommodityListView.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/20.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit

class CommodityView:UIView {
    
    var firstM:CommodityModel!
    var secondM:CommodityModel!
    
    init(firstModel:CommodityModel, secondModel:CommodityModel?) {
        super.init(frame: .zero)
        self.firstM = firstModel
        self.secondM = secondModel ?? CommodityModel()
        
        let iv1 = UIImageView()
        iv1.kf.setImage(with: URL(string: ActionURL.HOST + firstModel.url), placeholder: UIImage(named: "bg_place_holder"), completionHandler: nil)
        self.addSubview(iv1)
       
        let lblName1 = UILabel(text: firstModel.name, color: UIColor(hex:0x333333), fontSize: 11, isBold: false)
        self.addSubview(lblName1)
        
        let lblNum1 = UILabel(text: " X \(firstModel.num)", color: UIColor(hex:0x333333), fontSize: 11, isBold: false)
        self.addSubview(lblNum1)
        
        iv1.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(10.sp)
            make.width.equalTo(27.sp)
            make.height.equalTo(27.sp)
        }
        
        lblName1.snp.makeConstraints { (make) in
            make.centerY.equalTo(iv1)
            make.left.equalTo(iv1.snp.right).offset(5.sp)
        }
        
        lblNum1.snp.makeConstraints { (make) in
            make.centerY.equalTo(iv1)
            make.right.equalToSuperview()
            make.left.equalTo(lblName1.snp.right)
        }
        
        iv1.tag = 1
        lblName1.tag = 1
        lblNum1.tag = 1
        iv1.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
        lblName1.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
        lblNum1.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
        
        //如果第二个商品存在
        if let smodel = secondModel {
            
            let iv2 = UIImageView()
            iv2.kf.setImage(with: URL(string: ActionURL.HOST + smodel.url), placeholder: UIImage(named: "bg_place_holder"), completionHandler: nil)
            self.addSubview(iv2)
            
            let lblName2 = UILabel(text: smodel.name, color: UIColor(hex:0x333333), fontSize: 11, isBold: false)
            self.addSubview(lblName2)
            
            let lblNum2 = UILabel(text: " X \(smodel.num)", color: UIColor(hex:0x333333), fontSize: 11, isBold: false)
            self.addSubview(lblNum2)
            
            iv2.snp.makeConstraints { (make) in
                make.left.equalToSuperview()
                make.bottom.equalToSuperview().offset(-10.sp)
                make.width.equalTo(27.sp)
                make.height.equalTo(27.sp)
            }
            
            lblName2.snp.makeConstraints { (make) in
                make.centerY.equalTo(iv2)
                make.left.equalTo(iv2.snp.right).offset(5.sp)
                make.right.equalTo(lblNum2.snp.left)
            }
            
            lblNum2.snp.makeConstraints { (make) in
                make.centerY.equalTo(iv2)
                make.right.equalToSuperview()
                make.left.equalTo(lblName2.snp.right)
            }
            
            iv2.tag = 2
            lblName2.tag = 2
            lblNum2.tag = 2
            iv2.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
            lblName2.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
            lblNum2.addTapGesture(target: self, selector: #selector(pushToCommodityDetail(gesture:)))
            
        }
        
    }
    
    
    @objc func pushToCommodityDetail(gesture:UITapGestureRecognizer) {
        let tag = gesture.view?.tag ?? 1
        let detailVc = ProductController()
        detailVc.model = (tag == 1) ? firstM : secondM
        self.parentViewController?.navigationController?.pushViewController(detailVc, animated: true)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class CommodityListView: UIView {
    
    var pageControl: UIPageControl!
    var vwContent: UIScrollView!
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        
        vwContent = UIScrollView(frame: self.bounds)
        vwContent.isPagingEnabled = true
        vwContent.delegate = self
        vwContent.showsVerticalScrollIndicator = false
        vwContent.showsHorizontalScrollIndicator = false
        self.addSubview(vwContent)
        
        vwContent.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        pageControl = UIPageControl()
        pageControl.currentPage = 0
        pageControl.numberOfPages = 3
        pageControl.isHidden = true
        pageControl.pageIndicatorTintColor = UIColor.lightGray
        pageControl.currentPageIndicatorTintColor = UIColor.darkGray
        self.addSubview(pageControl)
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(10.sp)
            make.bottom.equalToSuperview()
        }
    }
    
    var arrCommodityView:[CommodityView] = [CommodityView]()
    
    var arrModel:[CommodityModel] = [CommodityModel]() {
        didSet {
            vwContent.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
            for vwTemp in arrCommodityView {
                vwTemp.removeFromSuperview()
            }
            arrCommodityView.removeAll()
            
            
            if arrModel.count < 3 {
                pageControl.isHidden = true
                pageControl.currentPage = 0
                pageControl.numberOfPages = 0
            } else {
                pageControl.isHidden = false
                pageControl.currentPage = 0
                pageControl.numberOfPages = Int(ceil(arrModel.count.cg / 2))
            }
            
            for i in 1...Int(ceil(arrModel.count.cg / 2)) {
                let index = (i-1)*2
                let firstModel:CommodityModel = arrModel[index]
                var secondModel:CommodityModel? = nil
                if i * 2 > arrModel.count {
                //当前最后序列超过数组，只取一个
                    secondModel = nil
                } else {
                    secondModel = arrModel[index + 1]
                }
                let tmpView = CommodityView(firstModel: firstModel, secondModel: secondModel)
                vwContent.addSubview(tmpView)
                tmpView.snp.makeConstraints { (make) in
                    make.width.equalToSuperview()
                    make.height.equalToSuperview()
                    make.top.equalToSuperview()
                    if i == 1 {
                        make.left.equalToSuperview()
                    } else if i * 2 > arrModel.count {
                        make.left.equalTo((arrCommodityView.last?.snp.right)!)
                        make.right.equalToSuperview()
                    } else {
                        make.right.equalToSuperview()
                    }
                }
                arrCommodityView.append(tmpView)
            }
            
        }
    }
    
}

extension CommodityListView: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if arrModel.count < 3 {
            return
        }
        let offset = scrollView.contentOffset
        pageControl.currentPage = Int(ceil(offset.x / self.bounds.width))
    }
}
